package com.intel.factorytest.ChamberMode;

import android.app.Activity;
import android.content.Intent;
import android.os.Vibrator;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Looper;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.widget.Button;
import android.widget.TextView;
import android.view.View;
import android.view.Window;
import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager;
import android.app.ActionBar;
import android.os.SystemProperties;

import com.intel.factorytest.R;
import com.intel.factorytest.util.LogUtils;
import com.intel.factorytest.util.Util;
import com.intel.factorytest.util.ShellUtilsPlus;
import com.intel.factorytest.ChamberMode.ChamberModeMainActivity;
import com.intel.factorytest.ChamberMode.ChambermodeBaseActivity;



public class VibrateTest extends ChambermodeBaseActivity {
    
    private final static int VIBRATE_TEST_MSG_UPDATE_UI = 1;
    private final static int VIBRATE_TEST_MSG_TIME_OUT = 2;

    private static Vibrator mVibrator = null;
    private final static long VIBRATOR_ON_TIME = 19000;
    private final static long VIBRATOR_OFF_TIME = 0;
    long[] pattern = {VIBRATOR_OFF_TIME, VIBRATOR_ON_TIME};

    private int mTimeout = 20000;    
    private VibrateDown mVibrateDown = null;

    protected Handler mHandler = new Handler() {
        public void handleMessage(android.os.Message msg) {
            switch (msg.what) {
                case VIBRATE_TEST_MSG_UPDATE_UI: {                    
                    removeMessages(VIBRATE_TEST_MSG_UPDATE_UI);
                    updateTitle("");
                    break;
                }
                case VIBRATE_TEST_MSG_TIME_OUT: {                    
                    removeMessages(VIBRATE_TEST_MSG_TIME_OUT);                    
                    Util.isVibrateTesting = false ;
                    SystemProperties.set("persist.sys.chamber_run","true");
                    finish();
                    break;
                }

                default:
                    break;
            }
        }
    };

    @Override
    public void onCreate(Bundle b) {
        super.onCreate(b);
        LogUtils.loge("onCreate");
        mVibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);
    }
    
    @Override
    public void onResume() {
        LogUtils.loge("onResume");
        if(mVibrator != null) {
            mVibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);
        }        
        SystemProperties.set("persist.sys.chamber_run","false");
        super.onResume();
    }

    @Override
    protected void startTest(){
        LogUtils.logi("startVibrateTest");
        Util.isVibrateTesting = true;
        updateTitle("Vibrator");
        sendDelayedMsg(VIBRATE_TEST_MSG_UPDATE_UI,5000,null,null,mHandler);
        if (mVibrator != null) {
            mVibrator.vibrate(pattern, 0);
            updateMessage("Vibrate start");
            if (mTimeout > 0){
                LogUtils.logi("startVibrateTest Vibrate 20s");
                new countDownThread().start();
            }
        }
    }

    @Override
    protected void stopTest(){
        LogUtils.logi("stopVibrateTest");
    }
    
    @Override
    public void onPause() {
        super.onPause();
        LogUtils.loge( "onPause() ");
        if (mVibrator != null) {
            mVibrator.cancel();
        }
        if ( mVibrateDown != null ) mVibrateDown.cancel();
    }

    @Override
    public void onStop() {
        super.onPause();
        LogUtils.loge( "onStop() ");
        if (mVibrator != null) {
            mVibrator.cancel();
        }
        if ( mVibrateDown != null ) mVibrateDown.cancel();
        if (mVibrator!= null) {
            mVibrator = null;
        }
        if (mVibrateDown != null) {
            mVibrateDown = null;
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        LogUtils.loge( "onDestroy() ");
        if (mVibrator!= null) {
            mVibrator = null;
        }
        if (mVibrateDown != null) {
            mVibrateDown = null;
        }
    }
    
    @Override
    public void finish() {
        LogUtils.logi("finish()");
        Intent i = new Intent();
        Bundle bundle = new Bundle();
        bundle.putString(ChamberModeMainActivity.VIBRATOR_RESULT, "vibrator_test_finish");        
        i.putExtras(bundle);
        setResult(ChamberModeMainActivity.START_VIBRATOR_TEST_RESULT_CODE,i);
        super.finish();
    }

    private class countDownThread extends Thread{
        public countDownThread(){}

        @Override
        public void run() {
            LogUtils.logi("countDownThread  run()");
            // TODO Auto-generated method stub
            Looper.prepare();
            mVibrateDown = new VibrateDown();
            mVibrateDown.start();
            Looper.loop();
        }
    }

    private class VibrateDown extends CountDownTimer{

        public VibrateDown() {
            super(mTimeout,mTimeout);
        }

        @Override
        public void onFinish() {
            // TODO Auto-generated method stub
            LogUtils.logi("Time Out:");
            mHandler.sendEmptyMessage(VIBRATE_TEST_MSG_TIME_OUT);
        }

        @Override
        public void onTick(long millisUntilFinished) {
            // TODO Auto-generated method stub
        }
    }

}

